#include <chrono>

#include "rclcpp/rclcpp.hpp"

using namespace std::literals::chrono_literals;

class ParamLogNode : public rclcpp::Node
{
public:
    ParamLogNode(std::string name) : Node(name)
    {
        RCLCPP_INFO(this->get_logger(), "I am %s", name.c_str());
        RCLCPP_INFO(this->get_logger(), "aaaaaaa\n, bbbbbbbb\n,ccccccc\n,ddddddd");
        this->declare_parameter("param_log_level", 0);
        if (!this->has_parameter("param_log_level"))
        {
            RCLCPP_INFO(this->get_logger(), "No parameter:param_log_level.");
            throw std::runtime_error("");
        }
        // this->set_parameter(rclcpp::Parameter("param_log_level",10));
        this->get_parameter("param_log_level", log_level_);
        this->get_logger().set_level(static_cast<rclcpp::Logger::Level>(log_level_));
        timer_ = this->create_wall_timer(500ms, std::bind(&ParamLogNode::_timer_callback, this));
    }

private:
    void _timer_callback()
    {
        this->get_parameter("param_log_level", log_level_);
        this->get_logger().set_level(static_cast<rclcpp::Logger::Level>(log_level_));
        std::cout << "=============================" << log_level_ << "================================" << std::endl;
        RCLCPP_DEBUG(this->get_logger(), "DEBUG log...");
        RCLCPP_INFO(this->get_logger(), "INFO log...");
        RCLCPP_WARN(this->get_logger(), "WARN log...");
        RCLCPP_ERROR(this->get_logger(), "ERROR log...");
        RCLCPP_FATAL(this->get_logger(), "FATAL log...");
    }

private:
    int log_level_{0};
    rclcpp::TimerBase::SharedPtr timer_{};
};

int main(int argc, char **argv)
{
    rclcpp::init(argc, argv);
    auto node = std::make_shared<ParamLogNode>("param_log_01");
    rclcpp::spin(node);
    rclcpp::shutdown();

    return 0;
}